<?
// myHandbook: High-level display functions
// Gives look-and-feel to the Web site
// Written by John Shepherd, May 2007
// Assumes that "lib/defs.php" already included

libs("forms","session");

// Functions related to pages

# logo:
# - returns HTML version of the logo (large or small)

function logo($size="large")
{
	$top = URL."/index.html";
	$style = "font-size:".(($size == "large") ? "20pt" : "16pt");
	return "<a href='$top' style='$style'>".SYSNAME."</a>";
}

# pageHeader:
# - build a standard MessageBoard page header
# - allows incorporation of style info and JavaScript

function pageHeader($title="",$style="",$styleMod="",$jsdefs="",$jsinit="")
{
	$html  = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">";
	$html .= "\n<html>\n<head>\n";
	if ($title != "") $html .= "<title>".SYSNAME.": $title</title>\n";
	$html .= "<meta http-equiv=\"Content-Type\" ";
	$html .= "content=\"text/html; charset=ISO-8859-1\">\n";
	$html .= "<meta http-equiv=\"Content-Script-Type\" ";
	$html .= "content=\"text/javascript\">\n";
	$html .= "<meta http-equiv=\"Pragma\" content='no-cache'>\n";
	$html .= "<meta http-equiv=\"expires\" content='0'>\n";
	if ($style != "") {
		$html .= "<link rel='stylesheet' type='text/css' ";
		$html .= "href='".URL."/lib/$style.css'>\n";
	}
	if (def($styleMod))
		$html .= "<style type='text/css'>\n$styleMod\n</style>\n";
	if (def($jsdefs))
		$html .= "$jsdefs\n";
	if (!def($jsinit))
		$html .= "</head>\n<body>\n";
	else {
		$html .= "<script type='text/javascript'>\n".
			 "function initOnLoad() {\n$jsinit\n}\n</script>\n".
			 "</head>\n<body onload='initOnLoad();'>\n";
	}
	return $html;
}

# startPage:
# - returns standard stuff to start HTML output
# - to ensure that all pages have the same look-and-feel

function startPage($titles="",$style="style",$styleMod="",
				$jsdefs="",$jsinit="",$keys="")
{
	//$searchForForm = (is_string($titles) && substr($titles,0,6) == "Select");
	$dontShowLogin = ($titles == "Login" || $titles == "Error");
	if (!$searchForForm) {
		if ($titles == "")
			$titles = array();
		elseif (!is_array($titles))
			$titles = array($titles=>"");
	}
	if ($searchForForm) {
		$htitle = $titles;
		$crumbs = logo("small")." : <b>$titles</b>";
	}
	else {
		$crumbs = logo(); $htitles = array();
		foreach ($titles as $title => $link) {
			if ($link != "")
				$t = hyperlink($link,$title)."\n";
			else
				$t = "<b>$title</b>\n";
			$crumbs .= " : $t";
			$htitles[] = $title;
		}
		$htitle = join(" : ",$htitles);
	}
	if ($searchForForm || $dontShowLogin) {
		$login = "";
		$menu = "";
		$div = "full";
	}
	else {
		$param = oneParam("returnto",thisURL());
		if (isLoggedIn()) {
			$name = userName(); $type = userType();
			$login = "Logged in as <b>$name</b> ($type)";
			$url = mkURL(URL."/session/logout.php",$param);
			$action = action($url,"Logout");
		}
		else {
			$login = "You are not logged into a lesson";
			$url = mkURL(URL."/session/login.php",$param);
			$action = action($url,"Login");
		}
		$login .= " $action\n";
		#$login .= "<br>Date: ".date("D M d")."\n";
		$menu = menu($keys);
		$div = "main";
	}
	$start = pageHeader($htitle,$style,$styleMod,$jsdefs,$jsinit);
	$heading = "<div class='header'>"."<div class='Lheading'>$crumbs<br>Date: ".date("D M d")."\n</div>".
			"<div class='Rheading'>$login</div></div>\n";
	return $start.$heading.$menu."<div class='$div'>\n";
}


# endPage:
# - returns HTML to finish off a page

function endPage()
{
	$start = getTimer();
	$end = microtime(true);
	$elapsed = sprintf("%0.1f", ($end - $start));
	return "</div>\n<div class='footer2'><form>".
"<b>Ask a question:</b>".
"<input type=\"text\" />".
"<input type=\"reset\" value=\"Reset\" />".
"<input type=\"submit\" value=\"Submit\" />".
"</form></div>\n</body>\n".jsTimer($elapsed)."\n</html>\n";
}


# menu:
# - build the menu in the left-hand-DIV

function menu($keys="")
{
	$menu = array("Week", "Lesson");
	if (isLoggedIn()) {
		if (isStudent())
			$menu[] = "enrolment";
		else
			$menu[] = "students";
	}
	$base = URL;
	$html = "<div class='menu'><p><b>Select a lesson</b></p>\n".startForm();
	/*foreach ($menu as $dir) {
		$label = ucFirst($dir);
		$html .= "<p>$label</p>\n";
	}*/
	$html .= /*"</ul>\n<p><b>Search:</b></p>\n".
		 startForm(URL."/search/index.php").inputText("keys",$keys,12).*/
		 lessonForm().p(inputSubmit("Submit!")).endForm().
		 /*"<p style='font-size:8pt'>".
		   "<b>Levels:</b><br>UG = Undergraduate<br>".
		   "PG = Postgrad Coursework<br>RS = Research</small></p>".*/
		 "<p style='font-size:9pt'>".
		   "Load time: <span id='timer'>0.0</span>s</p>".
		 "</div>";
	return $html;
}

function lessonForm()
{
   $comboboxes = "<b>Week:</b></br>".
            "<select>".
              "<option value =\"week01\">Week 1</option>".
              "<option value =\"week02\">Week 2</option>".
              "<option value =\"week03\">Week 3</option>".
            "</select></br>".
            "<b>Lesson:</b></br>".
            "<select>".
              "<option value =\"mon10001200\">MON 10:00-12:00</option>".
              "<option value =\"wed12001400\">WED 12:00-14:00</option>".
            "</select></br>";
   return $comboboxes;
}

# Timer:

$_starting = 0.0;
function setTimer() { global $_starting; $_starting = microtime(true); }
function getTimer() { global $_starting; return $_starting; }
function jsTimer($t)
{
return <<<_JS_
<script type='text/javascript'>
document.getElementById("timer").innerHTML= '$t';
</script>
_JS_;
}
?>
